<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | fromNow }}</p>
        </div>
        <van-button round   size="small" type="default" :class="{ active:article.is_collected }" @click="collectClick">
          <van-icon name="star-o" ></van-icon>
        </van-button>

        <van-button round size="small" type="info" @click="followClick">
          {{ article.is_followed === true ? '已关注' : '+ 关注' }}
        </van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <br />
      <br />
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude == 1 ? 'good-job' : 'good-job-o'"
          @click="togglePraise"
        >
          {{ article.attitude == 1 ? '取消点赞' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="article.attitude == 0 ? 'delete' : 'delete-o'"
          @click="toggleDisLike"
        >
          {{ article.attitude == 0 ? '取消不喜欢' : '不喜欢' }}
        </van-button>
      </div>
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
// 导入接口
import {
  getArticleInfo,
  followUser,
  unFollowUser,
  addPraise,
  cancelPraise,
  addDisLike,
  cancelDisLike,
  addCollect,
  cancelCollect
} from '@/api/home.js'

export default {
  name: 'myarticle',
  data () {
    return {
      article: {}
    }
  },
  async created () {
    const res = await getArticleInfo({ id: this.$route.query.id })
    this.article = res.data.data
  },
  methods: {
    /* 关注 */
    async followClick () {
      // 判断关注状态
      if (this.article.is_followed === true) {
        // 已关注，则取消关注
        await unFollowUser({ target: this.article.aut_id })
        this.$toast.success('取关成功')
        this.article.is_followed = false
      } else {
        // 没有关注，则关注
        await followUser({ target: this.article.aut_id })
        this.$toast.success('关注成功')
        this.article.is_followed = true
      }
    },
    /* 点赞 */
    async togglePraise () {
      // 根据attitude来决定调用哪个方法   1:点赞
      if (this.article.attitude === 1) {
        // 点击时, 要取消点赞
        const res = await cancelPraise(this.article.art_id)
        if (res.status === 204) {
          this.$toast.success('取消点赞成功')
          this.article.attitude = -1
        } else {
          this.$toast.success('操作失败')
        }
      } else {
        // 点击时, 要进行点赞请求
        const res = await addPraise(this.article.art_id)
        if (res.status === 201) {
          this.$toast.success('点赞成功')
          this.article.attitude = 1
        } else {
          this.$toast.success('操作失败')
        }
      }
    },
    /* 收藏 */
    async collectClick () {
      if (this.article.is_collected) {
        // 点击时, 要取消不喜欢
        const res = await cancelCollect(this.article.art_id)
        if (res.status === 204) {
          this.$toast.success('取消收藏成功')
          this.article.is_collected = false
        } else {
          this.$toast.success('操作失败')
        }
      } else {
        // 点击时, 要进行不喜欢
        const res = await addCollect(this.article.art_id)
        if (res.status === 201) {
          this.$toast.success('收藏成功')
          this.article.is_collected = true
        } else {
          this.$toast.success('操作失败')
        }
      }
    },
    /* 不喜欢 */
    async toggleDisLike () {
      // 根据attitude来决定调用哪个方法  0：不喜欢
      if (this.article.attitude === 0) {
        // 点击时, 要取消不喜欢
        const res = await cancelDisLike(this.article.art_id)
        if (res.status === 204) {
          this.$toast.success('取消不喜欢成功')
          this.article.attitude = -1
        } else {
          this.$toast.success('操作失败')
        }
      } else {
        // 点击时, 要进行不喜欢
        const res = await addDisLike(this.article.art_id)
        if (res.status === 201) {
          this.$toast.success('不喜欢成功')
          this.article.attitude = 0
        } else {
          this.$toast.success('操作失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  .van-button--default{
    margin-right: 20px;
    font-size: 15px;
  }
  .van-button--default.active{
    background-color: #fec635;
    color: #fff;
  }
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
